package com.gzx.plugin.tjzy.modular.call.single.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.call.single.entity.GzxSinglePointCalls;
import com.gzx.plugin.tjzy.modular.call.single.entity.vo.GzxSinglePointCallsVo;
import com.gzx.plugin.tjzy.modular.call.single.mapper.GzxSinglePointCallMapper;
import com.gzx.plugin.tjzy.modular.call.single.service.GzxSinglePointService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.page.PageQuery;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class GzxSinglePointServiceImpl implements GzxSinglePointService {

    private final GzxSinglePointCallMapper gzxSinglePointCallMapper;

    /**
     * 分页查询
     *
     * @param pageQuery。分页条件
     * @return 返回分页数据
     */
    @Override
    @DS("tjzy")
    public Page<GzxSinglePointCallsVo> pageList(PageQuery pageQuery) {
        LambdaQueryWrapper<GzxSinglePointCalls> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GzxSinglePointCalls::getCreateUser, StpLoginUserUtil.getLoginUser().getId());
        wrapper.orderByDesc(GzxSinglePointCalls::getDailingTime);
        return gzxSinglePointCallMapper.selectVoPage(new Page<>(pageQuery.getCurrent(), pageQuery.getSize()), wrapper);

    }

    /**
     * 插入数据
     *
     * @param callNum。手机号
     * @return
     */
    @Override
    @DS("tjzy")
    public void insertData(String callNum) {
        GzxSinglePointCalls details = new GzxSinglePointCalls();
        details.setPhoneNumber(callNum);
        details.setCreateUser(StpLoginUserUtil.getLoginUser().getId());
        details.setDailingTime(new Date());
        if (StpLoginUserUtil.getLoginUser().getName() != null) {
            details.setCreateName(StpLoginUserUtil.getLoginUser().getName());
        }
        gzxSinglePointCallMapper.insert(details);
    }

    @Override
    @DS("tjzy")
    public List<GzxSinglePointCallsVo> list() {
        LambdaQueryWrapper<GzxSinglePointCalls> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GzxSinglePointCalls::getCreateName, StpLoginUserUtil.getLoginUser().getName());
        wrapper.orderByDesc(GzxSinglePointCalls::getDailingTime);
        return gzxSinglePointCallMapper.selectVoList(wrapper);
    }
}
